#include<iostream>
#include<string>
#include<vector>
#include<queue>
using namespace std;

//https://leetcode.cn/problems/binary-tree-level-order-traversal/
struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode* left, TreeNode* right) : val(x), left(left), right(right) {}
};

class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root)
    {
        vector<vector<int>>vv;
        if (root == nullptr)
        {
            return vv;
        }


        int size = 0;
        queue<TreeNode*> q;
        q.push(root);
        size = 1;

        while (q.size())
        {
            vector<int> v;
            while (size--)
            {
                TreeNode* front = q.front();
                q.pop();

                if (front->left)
                {
                    q.push(front->left);
                }

                if (front->right)
                {
                    q.push(front->right);
                }
                v.push_back(front->val);
            }
            size = q.size();
            vv.push_back(v);
        }
        return vv;
    }
};